/*
 * Copyright (c) gitee.com/jiandong001.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import hilog from '@ohos.hilog';
import Context from '@ohos.application.context';
import { RequestHandler } from '../packagerConnection/RequestHandler';
import HashMap from '@ohos.util.HashMap';
import { BridgeDevSupportManager } from './BridgeDevSupportManager';
import { DisabledDevSupportManager } from './DisabledDevSupportManager';
import { SurfaceDelegateFactory } from '../common/SurfaceDelegateFactory';
import { DevBundleDownloadListener } from './DevBundleDownloadListener';
import { ReactContext } from '../bridge/ReactContext';
import { ReactInstanceDevHelper } from './ReactInstanceDevHelper';
import { DevSupportManager } from './DevSupportManager';
import { DevSupportManagerFactory } from './DevSupportManagerFactory';
/**
 * A simple factory that creates instances of {@link DevSupportManager} implementations. Uses
 * reflection to create BridgeDevSupportManager if it exists. This allows ProGuard to strip that
 * class and its dependencies in release builds. If the class isn't found, {@link
 * DisabledDevSupportManager} is returned instead.
 */
export class DefaultDevSupportManagerFactory implements DevSupportManagerFactory {
  private static readonly TAG: string = "React DefaultDevSupportManagerFactory"
  private static readonly DEVSUPPORT_IMPL_PACKAGE = "com.facebook.react.devsupport";
  private static readonly DEVSUPPORT_IMPL_CLASS = "BridgeDevSupportManager";

  //  public create(
  //  applicationContext: ReactContext, //Context
  //  reactInstanceDevHelper: ReactInstanceDevHelper,
  //  packagerPathForJSBundleName?: string,
  //  enableOnCreate?: boolean,
  //  minNumShakes?: number): DevSupportManager {
  //
  //  return create(
  //    applicationContext,
  //    reactInstanceDevHelper,
  //    packagerPathForJSBundleName,
  //    enableOnCreate,
  //    null,
  //    null,
  //    minNumShakes,
  //    null,
  //    null);
  //}

  //@Override
  public create(
    abilityContext: Context.AbilityContext,
    reactInstanceManagerHelper: ReactInstanceDevHelper,
    packagerPathForJSBundleName?: string,
    enableOnCreate?: boolean,
    //@Nullable RedBoxHandler redBoxHandler,
    devBundleDownloadListener?: DevBundleDownloadListener,
    minNumShakes?: number,
    customPackagerCommandHandlers?: HashMap<string, RequestHandler>,
    surfaceDelegateFactory?: SurfaceDelegateFactory): DevSupportManager {

    hilog.info(0x0000, DefaultDevSupportManagerFactory.TAG, 'create');

    if (!enableOnCreate) {
      return new DisabledDevSupportManager();
    }

    try {

      hilog.info(0x0000, DefaultDevSupportManagerFactory.TAG, 'create 2222');

      return new BridgeDevSupportManager(
        abilityContext,
        reactInstanceManagerHelper,
        packagerPathForJSBundleName,
        enableOnCreate,
        //          RedBoxHandler.class,
        devBundleDownloadListener,
        minNumShakes,
        customPackagerCommandHandlers,
        surfaceDelegateFactory);


      // ProGuard is surprisingly smart in this case and will keep a class if it detects a call to
      // Class.forName() with a static string. So instead we generate a quasi-dynamic string to
      // confuse it.
      //      String className =
      //          new StringBuilder(DefaultDevSupportManagerFactory.DEVSUPPORT_IMPL_PACKAGE)
      //              .append(".")
      //              .append(DefaultDevSupportManagerFactory.DEVSUPPORT_IMPL_CLASS)
      //              .toString();
      //      Class<?> devSupportManagerClass = Class.forName(className);
      //      Constructor constructor =
      //          devSupportManagerClass.getConstructor(
      //              Context.class,
      //              ReactInstanceDevHelper.class,
      //              String.class,
      //              boolean.class,
      //              RedBoxHandler.class,
      //              DevBundleDownloadListener.class,
      //              int.class,
      //              Map.class,
      //              SurfaceDelegateFactory.class);
      //      return (DevSupportManager)
      //          constructor.newInstance(
      //              applicationContext,
      //              reactInstanceManagerHelper,
      //              packagerPathForJSBundleName,
      //              true,
      //              redBoxHandler,
      //              devBundleDownloadListener,
      //              minNumShakes,
      //              customPackagerCommandHandlers,
      //              surfaceDelegateFactory);
    } catch (e) {
      let error  = e as Error
      throw new Error(
        "Requested enabled DevSupportManager, but BridgeDevSupportManager class was not found"
        + " or could not be created" + "message: " + error.message + "name: " + error.name + "stack: " + error.stack
        /*e*/
      );
    }
  }
}
